//-------------------WebPDFNativeScroll----------------------------
(function($, window, undefined) {
     'use strict';
    $.fn.WebPDFNativeScroll = function(settings) {
        function fwrJScrollPane(elem, s) {
            //elem = $('body'); // 测试去除doc viewer高度的时候滚动
            var settings, jsp = this;         
            var paneHeight, paneWidth;  
            function initialise(s) {
                //ios  书签滚动 
                elem.css({
                    'overflow': 'auto'
                });
                settings = s;
                paneHeight = elem.innerHeight();
                paneWidth = elem.innerWidth();
                initKeyboardNav();
            }

            function initKeyboardNav() { 
                if(!settings.keyDownCallback) return;    
                var keyDown;
                elem.attr('tabindex', 0).off('keydown.jsp keypress.jsp').on('keydown.jsp', function(e){                  
                    switch (e.keyCode) {
                        case 40:
                        case 38:
                        case 32:
                            keyDown = e.keyCode;
                            keyDownHandler();
                            break;
                        case 34:
                        case 33:
                        case 39:
                        case 37:
                        case 35:
                        case 36:
                            keyDown = e.keyCode;
                            keyDownHandler();
                            keyDown = null;
                            break;
                    }
                });

                function keyDownHandler() {                      
                        if (settings.keyDownCallback[keyDown] != null && typeof(settings.keyDownCallback[keyDown]) == "function") {
                            settings.keyDownCallback[keyDown]();
                        }
                 }
            }    

            function scrollToElement(ele, stickToTop, animate) {                    
                var e, eleHeight, eleWidth, eleTop = 0,
                    eleLeft = 0,
                    viewportTop, viewportLeft, maxVisibleEleTop, maxVisibleEleLeft, destY, destX;
                    var nDeltaOffsetY = arguments[3] ? arguments[3] : 0;
                try {
                    e = $(ele);
                } catch(err) {
                    return;
                }
                
                if (e.length < 1) return ;
                eleHeight = e.outerHeight();
                eleWidth = e.outerWidth();     
                eleTop = e.position().top;
                eleLeft = e.position().left;                

                viewportTop = 0;
                maxVisibleEleTop = viewportTop + paneHeight;
                if (eleTop < viewportTop || stickToTop) {
                 destY = eleTop;
                } else if (eleTop + eleHeight > maxVisibleEleTop) {
                    destY = eleTop - paneHeight + eleHeight;
                }                
                
                if (destY) {
                    scrollByY(destY + nDeltaOffsetY, animate);                   
                }
                viewportLeft = elem.scrollLeft();
                maxVisibleEleLeft = viewportLeft + paneWidth;
                if (eleLeft < viewportLeft || stickToTop) {
                    destX = eleLeft;
                } else if (eleLeft + eleWidth > maxVisibleEleLeft) {
                    destX = eleLeft - paneWidth + eleWidth;
                }
                if (destX) {
                    scrollByX(destX, animate);
                }
            }

            function scrollByX (deltaX, animate) {
                if (elem.scrollLeft() <= 0 && deltaX < 0) deltaX = 0;
                elem.scrollLeft(elem.scrollLeft() + deltaX);
            }

            function scrollByY (deltaY, animate) {                   
                if (elem.scrollTop() <= 0 && deltaY < 0) deltaY = 0;
                     elem.scrollTop(elem.scrollTop() + deltaY);
            }

            function scrollToX(destX, animate) {
                elem.scrollLeft(destX);
            }

            function scrollToY(destY, animate) { 
                
                elem.scrollTop(destY);                   
            }

            function Unimplemented(){
                $.error("Unimplemented function for native scroll bar.");
            }

            $.extend(jsp, {
                reinitialise: function(s) {
                    s = $.extend({},
                        settings, s);
                    initialise(s);
                },
                scrollToElement: function(ele, stickToTop, animate) {   
                    var nDeltaOffsetY = arguments[3] ? arguments[3] : 0;
                    scrollToElement(ele, stickToTop, animate, nDeltaOffsetY);                
                },
                scrollTo: function(destX, destY, animate) {
                    elem.scrollLeft(destX);
                    elem.scrollTop(destY);
                },
                scrollToX: function(destX, animate) {
                    scrollToX(destX, animate);
                },
                scrollToY: function(destY, animate) {
                   scrollToY(destY, animate);            
                },
                scrollToPercentX: function(destPercentX, animate) { Unimplemented();},
                scrollToPercentY: function(destPercentY, animate) { Unimplemented();},
                scrollBy: function(deltaX, deltaY, animate) {
                    jsp.scrollByX(deltaX, animate);
                    jsp.scrollByY(deltaY, animate);
                },
                scrollByX: function(deltaX, animate) {
                    scrollByX(deltaX, animate);
                },
                scrollByY: function(deltaY, animate) {                   
                    scrollByY(deltaY, animate);
                },
                positionDragX: function(x, animate) { Unimplemented(); },
                positionDragY: function(y, animate) {Unimplemented(); },
                animate: function(ele, prop, value, stepCallback) { Unimplemented(); },
                getContentPositionX: function() {
                    return elem.scrollLeft();
                },
                getContentPositionY: function() {
                    return elem.scrollTop();
                },
                getContentWidth: function() { return elem.width(); },
                getContentHeight: function() { return elem.height(); },
                getPercentScrolledX: function() { Unimplemented(); },
                getPercentScrolledY: function() { Unimplemented(); },
                getIsScrollableH: function() {
                    return jsp.getScrollBarHeight() > 0;
                },
                getIsScrollableV: function() {
                    return jsp.getScrollBarWidth() > 0;
                }
                ,
                getScrollBarWidth: function(){
                    return elem[0].offsetWidth - elem[0].clientWidth;
                },
                getScrollBarHeight: function(){
                    return elem[0].offsetHeight - elem[0].clientHeight;
                },
                getContentPane: function() {
                    return elem;
                },
                scrollToBottom: function(animate) { Unimplemented(); },
                hijackInternalLinks: function() { Unimplemented(); },
                destroy: function() { Unimplemented(); }
            });
        }
        return this.each(function() {
            var elem = $(this),
                jspApi = elem.data('jsp');
            if (jspApi) {
                jspApi.reinitialise(settings);
            } else {
                jspApi = new fwrJScrollPane(elem, settings);
                elem.data('jsp', jspApi);
            }
        });
    };
})(jQuery, this);